<template>
  <div id="app">
    <el-card class="left">
      <div class="btn">
        <el-button size="mini" @click="start" type="success">启动</el-button>
        <el-button size="mini" @click="reset" type="primary">重启</el-button>
        <el-button size="mini" @click="stop" type="warning">停止</el-button>
        <el-button size="mini" @click="clear" type="danger">清空</el-button>
      </div>
      <el-form :model="formData">
        <el-form-item label="启动端口" prop="port">
          <el-input v-model="config.port"></el-input>
        </el-form-item>
        <el-form-item v-for="item in list" :key="item.prop" v-bind="item">
          <el-input v-model.trim="formData[item.prop]" v-bind="item"></el-input>
        </el-form-item>
        <el-form-item label=" ">
          <el-button type="success" @click="add">添加</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-table :data="tableData" class="right" height="100%" border size="mini">
      <el-table-column v-for="item in list" :key="item.prop" v-bind="item" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="danger" @click="del(row)" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
try {
  var { ipcRenderer } = window.require("electron");
} catch (error) {
  console.log(error);
}
import { getStore, setStore } from "@xingyuelongchen/vue3-storage";
export default {
  name: "App",
  data() {
    return {
      ipcRenderer: null,
      config: getStore("config") || { port: 80, proxy: {} },
      formData: {},
      tableData: [],
      loading: false,
      list: [
        { prop: "key", label: "代理标识" },
        { prop: "name", label: "目标名称" },
        { prop: "target", label: "目标地址" }
      ]
    };
  },
  mounted() {
    this.getTableData();
    let that = this;
    ipcRenderer.on("resetServer", (event, data) => {
      that.$message.success(data);
    });
    ipcRenderer.on("startServer", (event, data) => {
      that.$message.success(data);
      that.loading = true;
    });
    ipcRenderer.on("stopServer", (event, data) => {
      that.$message.success(data);
      that.loading = false;
    });
  },
  methods: {
    getTableData() {
      this.tableData = Object.keys(this.config.proxy).map((e) => {
        return {
          key: e,
          target: this.config.proxy[e]
        };
      });
    },
    del(row) {
      let { key } = row;
      delete this.config.proxy[key];
      setStore("config", this.config);
      this.getTableData();
      this.$message.info("重启服务后生效！");
    },
    add() {
      let { key, target } = this.formData;
      this.config.proxy[key] = target;
      setStore("config", this.config);
      this.formData = {};
      this.getTableData();
      this.$message.info("重启服务后生效！");
    },
    clear() {
      this.config.proxy = {};
      setStore("config", this.config);
      this.getTableData();
      this.$message.info("重启服务后生效！");
    },
    reset() {
      if (this.loading) {
        ipcRenderer.send("resetServer", this.config);
      } else {
        this.$message.error("服务未运行，请启动服务！", "提示！");
      }
    },
    start() {
      console.log(this.loading);
      if (this.loading) return;
      console.log(this.config);
      ipcRenderer.send("startServer", this.config);
    },
    stop() {
      if (!this.loading) return;
      ipcRenderer.send("stopServer");
    }
  }
};
</script>

<style lang="less">
html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  padding: 20px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: stretch;
  height: 100%;
  overflow: auto;
  overflow-y: hidden;
  .left {
    flex: 3;
    min-width: 300px;
    margin-right: 20px;
    height: 100%;
  }
  .right {
    flex: 14;
    min-width: 600px;
    height: 100%;
  }
  .btn {
    margin-bottom: 40px;
  }
}
</style>
